trigger SyncAuditToCalendar on Audit__c (after insert,after update,before delete) {
    
    recordtype CallPlanRecordType=[select id,sobjecttype,developername from recordtype where sobjecttype=:'event' and developername=:'CallPlan' limit 1];
    
    if(system.trigger.isinsert){
        list<event> insertEvent=new list<event>();
        set<Date> SortDate=new set<date>();
        set<id> ownerId=new set<id>();
        
        for(audit__c t:trigger.new){
            Event e = new Event(Subject='拜访', 
                                        WhatId=t.id,
                                        recordtypeid=CallPlanRecordType.id,
                                        ownerid=t.Owner__c,
                                        StartDateTime=datetime.newInstancegmt(t.Plan_Date__c.year(),t.Plan_Date__c.month(),t.Plan_Date__c.day(), 0, 0, 0),
                                        EndDateTime=datetime.newInstancegmt(t.Plan_Date__c.year(),t.Plan_Date__c.month(),t.Plan_Date__c.day(), 0, 30, 0),
                                        IsReminderSet = false,
                                        eventDate__c=t.plan_date__c,
                                        hospitalname__c=string.valueof(t.sort__c)
                                         );
             insertEvent.add(e);
             sortDate.add(t.plan_date__c);
             ownerId.add(t.owner__c);
             
        }
        insert insertEvent;
        CalendarSorter.sort(sortDate,Ownerid);

    
    }
    
    if(system.trigger.isupdate){
        list<id> auditId=new list<id>();
        set<Date> SortDate=new set<date>();
        set<id> ownerId=new set<id>();
        map<id,date> updateAuditDateMap=new map<id,date>();
        map<id,string> AuditSortMap=new map<id,string>();
        for(audit__c t:trigger.new){
            if(trigger.oldmap.get(t.id).plan_date__c != t.plan_date__c ||  trigger.oldmap.get(t.id).sort__c != t.sort__c ){
                auditid.add(t.id);
                updateAuditDateMap.put(t.id,t.plan_date__c);
                AuditSortMap.put(t.id,string.valueof(t.sort__c));
                sortdate.add(t.plan_date__c);
                sortdate.add(trigger.oldmap.get(t.id).plan_date__c);
                ownerid.add(t.owner__c);
            }
        }
        if(auditid.size()>0){
            list<event> eventlist=[select id,whatid from event where whatid in: auditid];
            for(event e:eventlist){
                date auditDate;
                auditDate=updateAuditDateMap.get(e.whatid);
                e.hospitalname__c=auditSortMap.get(e.whatid);
                e.StartDateTime=datetime.newInstancegmt(auditDate.year(),auditDate.month(),auditDate.day(), 0, 0, 0);
                e.eventdate__c=auditdate;
            }
            update eventlist;
        }
        CalendarSorter.sort(sortDate,Ownerid);
    }
    
    if(system.trigger.isdelete){
        list<id> auditId=new list<id>();
        set<Date> SortDate=new set<date>();
        set<id> ownerId=new set<id>();
        for(audit__c t:trigger.old){
            auditid.add(t.id);
            sortdate.add(t.plan_date__c);
            ownerid.add(t.owner__c);
        }
        list<event> EventList=[select id from event where WhatId in: auditid];
        if(eventlist.size()>0){
            delete eventlist;
        }
        CalendarSorter.sort(sortDate,Ownerid);
    }
}